﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using uMES.LeanManufacturing.Common;
using uMES.LeanManufacturing.ParameterDTO;
using uMES.LeanManufacturing.ReportBusiness;

public partial class BwDispatchReportForm : PageBase
{
    int m_PageSize = 50;
    uMESReportBusiness reportBal = new uMESReportBusiness();
    protected void Page_Load(object sender, EventArgs e)
    {
        uMESMasterPage master = this.Master as uMESMasterPage;
        master.strNavigation = "当前位置：派工记录报表";
        master.strTitle = "派工记录报表";
        master.ChangeFrame(true);
        upageTurning.PageIndexChanged += new pageTurning.PageIndexChangedEventHandler(() => { SearchData(upageTurning.CurrentPageIndex.ToString()); });
        //WebPanel = WebAsyncRefreshPanel1;
        ClearMessage();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {

        try
        {
            gdResult.Rows.Clear();
            SearchData("1");
        }
        catch (Exception ex)
        {
            DisplayMessage(ex.Message, false);
        }
    }
    protected void btnReSet_Click(object sender, EventArgs e)
    {
        txtContainerName.Text = "";
        txtProcessNo.Text = "";
        txtSpecName.Text = "";
        txtProduct.Text = "";

        txtStartDate.Value = "";
        txtEndDate.Value = "";
        gdResult.Rows.Clear();
    }
    protected void btnExport_Click(object sender, EventArgs e)
    {
        try
        {
            ExportToExcel();
        }
        catch (Exception ex)
        {
            DisplayMessage(ex.Message, false);
        }
    }
    void SearchData(string index)
    {

        uMESPagingDataDTO result = reportBal.GetContainerDispatchInfo(GetQuery(index));

        this.gdResult.DataSource = result.DBTable;
        this.gdResult.DataBind();
        //给分页控件赋值，用于分页控件信息显示
        this.upageTurning.TotalRowCount = int.Parse(result.RowCount);
        this.upageTurning.RowCountByPage = m_PageSize;

    }

    protected Dictionary<string, string> GetQuery(string strIndex)
        {
        Dictionary<string, string> para = new Dictionary<string, string>();
        para["CurrentPageIndex"] = strIndex.ToString();
        para["PageSize"] = m_PageSize.ToString();

        para["ProcessNo"] = txtProcessNo.Text.Trim();
        para["ContainerName"] = txtContainerName.Text.Trim();
        para["Product"] = txtProduct.Text.Trim();
        para["StepName"] = txtSpecName.Text.Trim();
        para["StartDate"] = txtStartDate.Value.Trim();
        para["EndDate"] = txtEndDate.Value.Trim();
        return para;

    }

    void ExportToExcel() {

        // DataTable dt = uMESCommonFunction.GetGridData(gdResult);
        uMESPagingDataDTO result = reportBal.GetContainerDispatchInfo(GetQuery("all"));
        DataSet ds = new DataSet();

        DataView view = result.DBTable.DefaultView;

        result.DBTable = view.ToTable(false, "ProcessNo", "ProductName", "ProductDesc", "ContainerName", "conqty", "SpecNameDisp", "fullname", "dispatchdate", "operator", "teamname", "factoryname", "plannedcompletiondate");

        result.DBTable.Columns["ProcessNo"].ColumnName = "工作令号";
        result.DBTable.Columns["ProductName"].ColumnName = "图号";
        result.DBTable.Columns["ProductDesc"].ColumnName = "名称";
        result.DBTable.Columns["ContainerName"].ColumnName = "批次号";
        result.DBTable.Columns["ConQty"].ColumnName = "批次数量";
        result.DBTable.Columns["SpecNameDisp"].ColumnName = "工序名称";
        result.DBTable.Columns["fullname"].ColumnName = "班组长";
        result.DBTable.Columns["dispatchdate"].ColumnName = "派工时间";
        result.DBTable.Columns["operator"].ColumnName = "操作工";

        result.DBTable.Columns["teamname"].ColumnName = "所属班组";
        result.DBTable.Columns["factoryname"].ColumnName = "所属分厂";

        result.DBTable.Columns["plannedcompletiondate"].ColumnName = "要求完工时间";

        //NOPI方式导出
        DataTableToExcel excel1 = new DataTableToExcel();
        try
        {
            result.DBTable.TableName = "派工记录报表";
            ds.Tables.Add(result.DBTable);
            excel1.RenderToExcel(ds, Context,System.DateTime.Now.ToString("yyyyMMddhhss")+ ".xls");
        }
        catch(Exception ex)
        {
            DisplayMessage(ex.Message, false);
        }
    }
}